001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb; 021 022 import jgb.builder.Builder; 023 import jgb.builder.DefaultBuilder; 024 import jgb.builder.JgbEntityResolver; 025 import org.xml.sax.InputSource; 026 import org.xml.sax.SAXException; 027 028 import java.awt.*; 029 import java.io.*; 030 import java.util.Arrays; 031 import java.util.Iterator; 032 import java.util.LinkedList; 033 import java.util.List; 034 035 /** 036 * Builds a window from an XML file. 037 * <p> 038 * This tool may be used to view an XML file using the current implementation. 039 * </p> 040 * <h2>Usage</h2> 041 * <p> 042 * Simply run the following: 043 * </p> 044 * <pre> 045 * java jgb.BuildWindowFromXmlFile <em>path/to/xml-file</em> 046 * </pre> 047 * <p> 048 * Optional options are as follows: 049 * </p> 050 * <dl> 051 * <dt>--verbose</dt> 052 * <dt>-v</dt> 053 * <dd> 054 * Asks the {@link jgb.builder.DefaultBuilder Builder} to be 055 * {@linkplain jgb.builder.DefaultBuilder#setVerbose(boolean) verbose}. 056 * </dd> 057 * <dt>--quiet</dt> 058 * <dt>-q</dt> 059 * <dd> 060 * Asks the {@link jgb.builder.DefaultBuilder Builder} to be 061 * {@linkplain jgb.builder.DefaultBuilder#setQuiet(boolean) quiet}. 062 * </dd> 063 * <dt>--show</dt> 064 * <dd> 065 * Asks the JGB framework to pack and show the window after it is read. 066 * </dd> 067 * </dl> 068 * @since 0.1a 069 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 070 */ 071 public class BuildWindowFromXmlFile { 072 private boolean showAtEnd; 073 074 ///CLOVER:OFF 075 /** 076 * Builds a window from the file passed as the first argument. 077 * @param args The arguments as received from the command-line. 078 * @throws Exception If an exception is thrown while building the window. 079 */ 080 public static void main(String[] args) throws Exception { 081 final BuildWindowFromXmlFile processor = new BuildWindowFromXmlFile(); 082 final Builder builder = new DefaultBuilder(); 083 builder.addResolver(new JgbEntityResolver()); 084 085 final String path = processor.processArguments(builder, Arrays.asList(args)); 086 087 InputSource in = new InputSource(); 088 in.setByteStream(new BufferedInputStream(new FileInputStream(path))); 089 in.setCharacterStream(new BufferedReader(new FileReader(path))); 090 in.setSystemId(new File(path).toURL().toString()); 091 092 processor.start(builder,in); 093 } 094 ///CLOVER:ON 095 096 public void printUsage(PrintWriter out, InvalidParameterException exception) { 097 out.print("Error: "); 098 out.println(exception.getMessage()); 099 out.println("Usage:"); 100 out.println(" java jgb.BuildWindowFromXmlFile [--verbose|-v] [--show] <xml file>"); 101 out.println("where"); 102 out.println(" --show is asking for the packing and setting "); 103 out.println(" the visiblity of the built window"); 104 out.println(" --verbose or -v is asking for the Builder to be verbose"); 105 out.println(" --quiet or -q is asking for the Builder to be quiet"); 106 out.println(" and <xml file> is the path to an XML file which should be processed"); 107 } 108 109 /** 110 * Reads and processes each argument on the command line. 111 * @param builder The builder which will have its options set according 112 * to the options set on the command line. 113 * @param args The list of arguments as received from the command-line. 114 * The list will in no way be modified by this method. 115 * @throws RequiredArgumentMissingException if the filename argument is missing 116 * from the command line. 117 * @throws UnknownArgumentException if an argument cannot be processed 118 * because it is not a filename. 119 * @throws FileNotFoundException if the filename argument cannot be found 120 * on disk. 121 * @throws TooManyArgumentsException if there are more than one filename 122 * arguments on the command line. 123 * @return The absolute path to the last argument (filename). 124 */ 125 public String processArguments(final Builder builder, final List args) throws InvalidParameterException, FileNotFoundException { 126 final List copiedArgs = new LinkedList(args); 127 for (Iterator iterator = copiedArgs.iterator(); iterator.hasNext();) { 128 final String arg = (String)iterator.next(); 129 130 if (arg.equals("--verbose") 131 || arg.equals("-v")) { 132 builder.setVerbose(true); 133 } else if (arg.equals("--quiet") 134 || arg.equals("-q")) { 135 builder.setQuiet(true); 136 } else if (arg.equals("--show")) { 137 showAtEnd = true; 138 } else if (arg.startsWith("-")) { 139 throw new UnknownArgumentException(arg); 140 } else { 141 continue; 142 } 143 144 // Remove the now processed argument from the list 145 iterator.remove(); 146 } 147 148 if (copiedArgs.size() == 0) { 149 throw new RequiredArgumentMissingException("filename"); 150 } else if (copiedArgs.size() > 1) { 151 throw new TooManyArgumentsException(); 152 } 153 154 File file = new File((String)copiedArgs.get(0)); 155 if (false == file.exists()) { 156 throw new FileNotFoundException( 157 "File not found: '" + file.getName() + '\''); 158 } 159 160 return file.getAbsolutePath(); 161 } 162 163 public Component start(Builder b, InputSource in) throws IOException, SAXException { 164 return b.build(in); 165 } 166 }